package com.biz.primus.ms.oms.dao.specification;

import com.biz.primus.common.utils.StringUtil;
import com.biz.primus.model.oms.vo.OrderReturnQueryVo;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturn;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @author LGJ
 * @date
 */
public class OmsOrderReturnSpecification implements Specification<OmsReturn> {

    private OrderReturnQueryVo query;

    public OmsOrderReturnSpecification(OrderReturnQueryVo query){
        this.query = query;
    }

    @Override
    public Predicate toPredicate(Root<OmsReturn> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> predicates = Lists.newArrayList();

        if(!StringUtil.isNullOrEmpty(this.query.getDepotCode())){
            predicates.add(cb.equal(root.get("depotCode").as(String.class), this.query.getDepotCode()));
        }

        if(CollectionUtils.isNotEmpty(this.query.getReturnStatus())){
            predicates.add(root.get("returnState").in(this.query.getReturnStatus()));
        }

        if(!StringUtil.isNullOrEmpty(this.query.getOrderCode())){
            predicates.add(cb.equal(root.get("orderCode").as(String.class),this.query.getOrderCode()));
        }


        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return query.getRestriction();
    }
}
